﻿using System;
using System.Data.Entity;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using MM.Services.decl;
using MM.Services.impl;
using MM.Web.App_Start;
using MM.Data.DataContexts;
using Ninject;
using Ninject.Web.Common;

namespace MM.Web
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    /// <summary>
    /// 
    /// </summary>
    public class MvcApplication :  NinjectHttpApplication
    {
        /// <summary>
        /// Application_s the start.
        /// </summary>
        protected override void OnApplicationStarted()
        {
            base.OnApplicationStarted();

            Database.SetInitializer(new DropCreateDatabaseIfModelChanges<MyMoneyContext>());
            //Database.SetInitializer(new CreateDatabaseIfNotExists<MyMoneyContext>());

            AreaRegistration.RegisterAllAreas();

            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
        }

        /// <summary>
        /// Creates the kernel that will manage your application.
        /// </summary>
        /// <returns>
        /// The created kernel.
        /// </returns>
        protected override IKernel CreateKernel()
        {
            var kernel = new StandardKernel();
            try
            {
                kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
                kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();

                RegisterServices(kernel);
                //GlobalConfiguration.Configuration.DependencyResolver = new NinjectDependencyResolver(kernel);
                return kernel;
            }
            catch
            {
                kernel.Dispose();
                throw;
            }
        }

        /// <summary>
        /// Load your modules or register your services here!
        /// </summary>
        /// <param name="kernel">The kernel.</param>
        private static void RegisterServices(IKernel kernel)
        {
            kernel.Bind<IBalanceInterface>().To<BalanceService>();
            kernel.Bind<ICurrencyService>().To<CurrencyService>();
            kernel.Bind<IJournalPayment>().To<JournalPaymentService>();
            kernel.Bind<IOperationTypeInterface>().To<OperationTypeService>();
            kernel.Bind<IPrioritetInterface>().To<PrioritetService>();
        }   
    }
}